package com.itheima.health.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @Author likepei
 * @Date 2020/2/10 11:11
 * @Version v1.0
 * @Description 七牛云工具类
 */
public class QiniuUtils {
    //生成上传凭证，然后准备上传
    public static String qiniu_img_url_pre = "http://q5gsp5gz2.bkt.clouddn.com/"; //域名
    public static String accessKey = "L-Ie0yUVH35rgl3X9j7RFAdPsdLSq5hxneGqRER3"; //公钥
    public static String secretKey = "ZAcufVAu9jEwa8PAI9vlvMw8uiCdWxK6POrOhdrF";//私钥
    public static String bucket = "362-itcast-health"; //空间名称

    /**
     * 文件上传
     * @param uploadBytes 文件内容,字节数组
     * @param uploadFileName 保存到服务器端的文件名
     */
    public static void upload2QiNiu(byte[] uploadBytes, String uploadFileName){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);

        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = uploadFileName;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(uploadBytes, key, upToken);
            //解析上传成功的结果
            System.out.println(response.bodyString());

            //打印访问图片的路径
            System.out.println(qiniu_img_url_pre + uploadFileName);

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }

    }

    /**
     * 删除七牛云空间中的文件
     * @param filename 服务端文件名
     */
    public static void deleteFile2QiNiu(String filename){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释

        String key = filename;

        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }

    public static void main(String[] args) throws IOException {
        /*
        //测试 文件上传
        String localFilePath = "D:/IdeaProduct/test.jpg";
        FileInputStream fileInputStream = new FileInputStream(localFilePath);
        byte[] data = new byte[1024 * 1024]; //1M
        fileInputStream.read(data);
        String saveFileName = UUID.randomUUID().toString().replace("-", "");
        QiniuUtils.upload2QiNiu(data, saveFileName);
        */

        //删除七牛云空间中的文件
        deleteFile2QiNiu("b5795c639f3d4878a768b45935dd97d0");
    }
}
